package org.example.netty.test.nio.netty.chat.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.bean.GetAddUserMessage;
import org.example.netty.test.nio.netty.chat.session.UserSession;
import org.example.netty.test.nio.netty.chat.util.NettyChannelUtil;

import java.util.List;
import java.util.Set;

/**
 * @author zp
 * @version 1.0
 * @description: 处理 已加入的私聊的消息
 * @date 2023/4/17 19:27
 */
@Slf4j
public class GetAddUserMessageHandler extends SimpleChannelInboundHandler<GetAddUserMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GetAddUserMessage msg) throws Exception {
        Channel channel = ctx.channel();
        log.debug("查询已加入私聊的消息：{}",msg);
        UserSession userSession = NettyChannelUtil.getUserSession(channel);
        assert userSession != null;
        Set<String> chatList = userSession.getChatNameList();
        msg.setAddUserChatNames(chatList);
        channel.writeAndFlush(msg);
    }
}
